package beans.thread.lululu;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class socketServer
{
	private ServerSocket ss;
	private Socket socket;
	@SuppressWarnings("unused")
	private BufferedReader in;
	@SuppressWarnings("unused")
	private PrintWriter out;

	public socketServer()
	{
		try
		{
			ss = new ServerSocket(9999);

			System.out.println("The server is waiting your input...");

			while (true)
			{
				socket = ss.accept();

				DataInputStream reader = new DataInputStream(socket.getInputStream());

				byte[] bs = new byte[8];
				for (int i = 0; i < 8; i++)
				{
					byte b = reader.readByte();
					bs[i] = b;
				}

				String line = new String(bs, "UTF-8");
				System.out.println(line);

				BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
				bw.write(line);

				bw.close();
				reader.close();
				socket.close();

				if (line.equalsIgnoreCase("quit") || line.equalsIgnoreCase("exit"))
					break;
			}

			ss.close();

		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
}
